package com.hxzy.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.resp.R;
import com.hxzy.controller.front.contamer.req.FrontContamerReq;
import com.hxzy.controller.mobile.login.req.MobileLoginReq;
import com.hxzy.controller.mobile.login.resp.MobileLoginResp;
import com.hxzy.entity.Contamer;
import com.hxzy.service.ContamerService;
import com.hxzy.mapper.ContamerMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 11452
* @description 针对表【contamer(前端用户)】的数据库操作Service实现
* @createDate 2023-08-28 11:54:55
*/
@Service
public class ContamerServiceImpl extends ServiceImpl<ContamerMapper, Contamer>
    implements ContamerService{

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public long accountCount(String account) {
        LambdaQueryWrapper<Contamer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Contamer::getLoginId,account);

        return this.count(queryWrapper);
    }

    @Override
    public boolean register(FrontContamerReq req) {
        if(!req.getPassword().equals(req.getPasswordConfirm())){
            throw new ServiceException(AckCode.SET_PASSWORD_INVALID);
        }

        //再判断验证码
        String redisKey= RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY,req.getUuid());
        Object redisValue = this.redisTemplate.opsForValue().get(redisKey);
        if(Objects.isNull(redisValue)){
            throw new ServiceException(AckCode.CODE_NOT_FOUND);
        }

        //删除验证码
        this.redisTemplate.delete(redisKey);

        if(!redisValue.toString().equalsIgnoreCase(req.getCode())){
            throw new ServiceException(AckCode.CODE_ERROR);
        }

        //验证手机号是否存在
        long count=this.accountCount(req.getLoginId());
        if(count>0){
            throw new ServiceException(AckCode.PHONE_NUMBER_SET_ACCOUNT_INVALID);
        }

        //使用对象copy,反射
        Contamer contamer=new Contamer();
        BeanUtils.copyProperties(req,contamer);

        contamer.setStatus("0");
        contamer.setCreateBy(contamer.getUserName());
        contamer.setPassword(BCrypt.hashpw(contamer.getPassword()));

        boolean save = this.save(contamer);
        return save;
    }

    /**
     * 登录
     * @param req
     * @return
     */
    @Override
    public MobileLoginResp login(MobileLoginReq req) {
        LambdaQueryWrapper<Contamer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Contamer::getLoginId,req.getLoginName());

        Contamer db = this.getOne(queryWrapper);
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
        }

        if(!BCrypt.checkpw(req.getLoginPwd(),db.getPassword())){
            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
        }

        if(db.getStatus().equalsIgnoreCase("1")){
            throw new ServiceException(AckCode.DEVICE_BANNED);
        }


        MobileLoginResp loginResp=new MobileLoginResp();
        loginResp.setId(db.getId());
        loginResp.setLoginId(db.getLoginId());
        loginResp.setGender(db.getPortrait());
        loginResp.setGender(db.getGender());
        loginResp.setUserName(db.getUserName());


        return loginResp;
    }
}




